
package com.javafactroy.kunlong.codegen.context;


import com.javafactroy.kunlong.codegen.app.BasePanel;
import com.javafactroy.kunlong.codegen.util.DBHelper;
import com.javafactroy.kunlong.codegen.ui.MainFrame;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class AppContext {
    private static MainFrame app;

    static String configPath = System.getProperty("user.dir");
    static String codejenProp = configPath+"/config/codegen.properties";
    static String fileHbc = configPath+ "/config/default.hbc";

    private static Properties props = new Properties();
    private static String path = System.getProperty("user.dir") + "\\";


    private static Map<String, Properties> projPropertyMap = new HashMap<>();

    private static String currentProjectName = null;

    public static void setApp(MainFrame app) {
        AppContext.app = app;
    }

    public static MainFrame getApp() {
        return app;
    }


    public static <T extends BasePanel> T activePanel(Class<T> panel) {
        app.getWorkspacePanel().removeAll();
        /*  37 */
        app.getWorkspacePanel().updateUI();

        try {
            BasePanel basePanel = (BasePanel) panel.newInstance();
            /*  40 */
            app.getWorkspacePanel().add(basePanel, "Center");
            /*  41 */
            app.getWorkspacePanel().updateUI();
            /*  42 */
            return (T) basePanel;
            /*  43 */
        } catch (Exception e) {
            /*  44 */
            throw new RuntimeException(e);

        }

    }


    public static void init() throws IOException {
        /*  52 */
        System.out.println(codejenProp);
        File cf = new File(codejenProp);
        /*  53 */
        if (!cf.exists()) {
            /*  54 */
            cf.createNewFile();

        }
        /*  56 */
        props.load(new FileInputStream(cf));

        /*  58 */
        File hbc = new File(fileHbc);
        /*  59 */
        if (!cf.exists()) {
            /*  60 */
            cf.createNewFile();

        }
        /*  62 */
        Properties pProps = new Properties();
        /*  63 */
        pProps.load(new FileInputStream(hbc));

        /*  65 */
        String pname = pProps.getProperty("project.name", "default");
        /*  66 */
        System.out.println(pProps);
        /*  67 */
        saveProjectProperties(pProps, pname);
    }


    public static void saveProperties(Properties props) {
        /*  71 */
        AppContext.props.putAll(props);

        try {
            /*  73 */
            FileOutputStream out = new FileOutputStream(new File(codejenProp));//path + "codegen.properties"));
            /*  74 */
            AppContext.props.store(out, "配置文件");
            /*  75 */
            out.close();
            /*  76 */
        } catch (IOException e) {
            /*  77 */
            throw new RuntimeException("持久化配置异常:" + e.getMessage(), e);

        }

    }


    public static void saveProjectProperties(Properties props, String project) {

        setCurrentProject(project);

        Properties projProps = projPropertyMap.get(project);

        if (projProps == null) {
            /*  84 */
            projProps = new Properties();
            /*  85 */
            projPropertyMap.put(project, props);

        }
        /*  87 */
        projProps.putAll(props);


        try {
            /*  90 */
            FileOutputStream out = new FileOutputStream(new File(fileHbc));


            projProps.store(out, "配置文件");
            out.close();
            /*  93 */
        } catch (IOException e) {

             throw new RuntimeException(" " + e.getMessage(), e);

        }

    }


    /*  98 */
    public static Properties getProps() {
        return props;
    }

    public static void setCodegenHomepath(String homepath) {

        getCurrentProjectProps().setProperty("project.destDir", homepath);
        System.out.println(getCurrentProjectProps().getProperty("project.destDir", "/home-def/codegen"));

    }

    public static void mkdirs() {
        getCurrentProjectProps().getProperty("project.destDir", "/home/codegen");

    }

    public static File getGenerateTargetFile(String pkg, String fileName) {
        String basepath = getCurrentProjectProps().getProperty("project.destDir", "/home/codegen");
        String path = pkg.replaceAll("\\.", "/");
        path = basepath + "/" + path;
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        return new File(dir, fileName);

    }
//    public static File getGenerateTargetFile(String pkg, String fileName) {
//        String basepath=getCurrentProjectProps().getProperty("project.destDir", "/home/codegen");
//        File dir = new File(basepath);
//        String[] arr = pkg.split("\\.");
//        byte b;
//        int i;
//        String[] arrayOfString;
//
//        for (i = (arrayOfString = arr).length, b = 0; b < i; ) {
//            String tmp = arrayOfString[b];
//            dir = new File(dir, tmp);
//            if (!dir.exists())
//                dir.mkdirs();
//
//            b++;
//        }
//
//        return new File(dir, fileName);
//
//    }


    public static void flush() {
        DBHelper.reset();
    }


    public static Properties getCurrentProjectProps() {

        Properties projProps = projPropertyMap.get(getCurrentProject());

        if (projProps == null) {

            projProps = new Properties();

        }

        return projProps;

    }


    public static String getCurrentProject() {
        return currentProjectName;
    }


    public static void setCurrentProject(String projName) {
        currentProjectName = projName;
    }

    public static void loadProjectProperties(File file) {

        try {
            Properties props = getCurrentProjectProps();
            props.load(new FileInputStream(file));
            String projectName = props.getProperty("project.name");

            saveProjectProperties(props, projectName);
            /* 141 */
            setCurrentProject(projectName);
        } catch (Exception e) {
            throw new RuntimeException(e);

        }

    }

}


/* Location:               hbatis-codegen.jar!\com\pitaya\codegen\AppContext.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.2
 */